﻿@{
    ViewBag.Title = "PatAccountInfo";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="topPanel" style="margin-bottom: 1%;">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control form-control-focus" placeholder="请输入卡号、住院号、姓名" style="width: 200px;">
                    </div>
                </td>
                <td style="padding-left:12px;">
                    <div class="btn-group" id="btnKeywordTypeGourp" style="margin-top:0;">
                        <button type="button" data-keywordtype="kh" class="btn active btn-default">&nbsp;卡&nbsp;号&nbsp;</button>
                        <button type="button" data-keywordtype="zyh" class="btn btn-default">住院号</button>
                        <button type="button" data-keywordtype="xm" class="btn btn-default">&nbsp;姓&nbsp;名&nbsp;</button>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<!--基本信息-->
@Html.Partial("PatBasic")  
<!--收支清单-->
<form id="formPat2"> 
        <div class="gridPanel" style="margin-top:10px;border-radius:5px;">
            <table id="gridList" rel="formPat2"></table>
        </div> 
</form>
<!--预交款充值退费-->
@Html.Partial("AccAddPay")
<!--按钮工具条-->
<div class="toolbar toolbarfloat">
    <a id="acc_addMoney" class="btn btn-primary" onclick="PayDeposit()"><i class="fa fa-plus"></i> 充值</a>
    <a id="acc_reMoney" class="btn btn-primary" onclick="Refund()"><i class="fa fa-minus"></i> 退款</a>
    <a class="btn btn-primary" onclick="newtouch_globalevent_f5()"><span class="glyphicon glyphicon-refresh"> 选择账户</span></a>
</div>
<style>
    #yeCon{
        padding-left:25px;
        color:red; 
    }
</style>

<script>
    $(function () {
        $('#btnKeywordTypeGourp').on('click', '.btn', function () {
            if (!$(this).hasClass('active')) {
                $(this).siblings('.btn').removeClass('btn-restore').removeClass('active').addClass('btn-default');
                $(this).removeClass('btn-default').addClass('btn-restore active');
            }
        });
        //搜索框
        var txt_keyword = document.getElementById('txt_keyword');
        txt_keyword.addEventListener('keydown', function (event) {
            event = event || window.event;
            if (event.keyCode == 13) {
                searchPatInfo();
            }
        });

        gridListData([]);
        $(window).resize(function () {
            initLayout("formPat2");
        });
    });
     

    //回车\查询事件
    function searchPatInfo() {
        $.loading(true, "正在请求数据..."); 
        var keyword = $.trim($("#txt_keyword").val());
        if (keyword == "") {
            $.modalAlert("请输入有效住院号、卡号、或姓名", 'warning');
            return false;
        }
        var zyh = null;
        var kh = null;
        var xm = null;
        if ($('#btnKeywordTypeGourp .btn.active').attr('data-keywordtype') == 'kh') {
            kh = keyword;
        }
        else if ($('#btnKeywordTypeGourp .btn.active').attr('data-keywordtype') == 'zyh') {
            zyh = keyword;
        }
        else {
            xm = keyword;
        }
       
        var patobj = new Object();
        patobj.kh = kh;
        patobj.zyh = zyh;
        patobj.xm = xm;
        getPatInfoAjax(patobj);
    }

    //后台ajax查询人员数据
    function getPatInfoAjax(patobj) { 
        $.najax({
            type: "POST",
            url: "/PatientManage/AccountManage/PatAccountInfo",
            data: { zyh: patobj.zyh, kh: patobj.kh, xm: patobj.xm },
            dataType: "json",
            cache: false,
            success: function (ajaxresp) {
                $.loading(false);
                var patInfoList = ajaxresp.data.patInfoList;
                if (patInfoList && patInfoList.length > 0) {
                    if (patInfoList.length == 1) {
                        setPatInfoModel(ajaxresp.data.patInfo);
                        gridListData(ajaxresp.data.accPayInfoList);
                    } else {
                        //弹出加载选择人员的界面窗口
                        GetPatSerarchView(patobj.zyh, patobj.xm, patInfoList);
                    }
                }
            },
            error: function (err) {
                $.loading(false);
            },
            complete: function () {
                $.loading(false);
            }
        });
    }


    //获取病人基本信息界面显示
    function setPatInfoModel(patModel) { 
        $('#formPat1').formSerialize(patModel);
        //获取网页中的单选按钮，挑选其中name=angel，value=0的，让其选中
        //$("input[type=radio][name=xb][value=" + patmodel[idname] + "]").attr("checked", 'checked');
        $("#xb").val($.getGender(patModel.xb));
        $("#zhsyr").val(patModel.xm);//所有人
        $("#ryrq").val((patModel.ryrq && patModel.ryrq.length >= 10 ? patModel.ryrq.substring(0, 10) : ""));
        var nl = !(patModel.csny == null || undefined) ? GetAgeByCsny(patModel.csny) : "";
        $("#nl").val(nl);
        $("#txt_keyword").val("");
    }

    //账户收支列表
    function gridListData(mydata) { 
        $('#gridList').jqGrid("clearGridData");
        var $gridList = $("#gridList");
        var zhye = 0;
        if (mydata != [] && mydata.length > 0) {
            zhye = mydata[0].zhye;
            //zhye = zhye.toFixed(2);
        } 
        $("#zhye").val(zhye); //value赋值
        var captionCon = "账户收支清单";
        captionCon += "<span id='yeCon'>账户余额：" + zhye + "元</span>"; 
        $gridList.dataNewGrid({
            height: 150, 
            autowidth: false,
            width: $("#formPat2").width() - 10,
            caption: captionCon,
            colModel: [
                { label: '主键', name: 'patid', hidden: true, key: true },
                { label: '收支金额', name: 'szje', width: 105, align: 'left'},
                //{
                //    label: '收支金额', name: 'szje', width: 105, align: 'left', formatter: function (cellvalue) {
                //        return cellvalue ? cellvalue.toFixed(2) : "0.00";
                //      }
                //},
                {   label: '账户金额', name: 'zhye', width: 105, align: 'left'},
                //{
                //    label: '账户金额', name: 'zhye', width: 105, align: 'left', formatter: function (cellvalue) {
                //        return cellvalue ? cellvalue.toFixed(2) : "0.00";
                //    }
                //},
                { label: '凭证号', name: 'pzh', width: 105, align: 'left' },
                { label: '收支人员', name: 'CreatorUserName', width: 105, align: 'left' },
                { label: '支付方式', name: 'xjzffsmc', width: 105, align: 'left' },
                { label: '收支日期', name: 'CreateTime', width: 200, align: 'left' },
                { label: '收支性质', name: 'szxz', width: 105, align: 'left' }
            ]
        }, mydata);
        
        if (mydata.length>0) {
            $('.ui-jqgrid-bdiv').find(".unwritten").remove();
        }
        $("#yeCon").text("账户余额：" + zhye + "元");
    }

    var patid,
        zyh,
        zh,
        zhxz,
        zhbh,
        zffsbh,
        zffsmc,
        zfmoney,
        pzh;
    //充值和退款判断
    function CheckDeposit() { 
        patid = $("#patid").val();
        zyh = $("#zyh").val();
        zh = $("#zh").val();
        zhxz = $("#zhxz").val();
        zhbh = $("#zhbh").val();
        if (zyh == "" || patid == "" || zh == "" || zhbh == "" || zhxz == "") {
            $.modalAlert("病人基本信息不能为空", 'warning');
            return false;
        }
        //var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        var reg = /^(-?\d+)(\.\d+)?$/;
        zfmoney = $("#txtmoney").val();
        var ddlzffs = $("#ddlzffs option:selected");//获取选中的项
        zffsbh = ddlzffs.val();//拿到选中项的值
        zffsmc = ddlzffs.text();//拿到选中项的文本
        //pzh = $("#lblpzh").text();
        pzh = $("#txtlpzh").val();
        if (zffsbh == -1 || zffsmc == "") { 
            $.modalAlert("支付方式必选", 'warning');
            return false;
        }
        if (zfmoney == "" || zfmoney == undefined) {
            $.modalAlert("支付金额必填", 'warning');
            return false;
        } else {
            if (!reg.test(zfmoney)) {  
                $.modalAlert("请输入正确的金额格式", 'warning');
                return false;
            }
        }
        if (pzh == "" || pzh == undefined) { 
            $.modalAlert("凭证号必选", 'warning');
            return false;
        }
        return true;
    }

    //充值前台验证金额和是否选择支付类型
    function PayDeposit() { 
        zfmoney = $("#txtmoney").val();
        if(parseFloat(zfmoney)<0)
        {
            $.modalAlert("支付金额必填", 'warning'); 
            return false;
        }
        if (CheckDeposit())
        {
            PayDepositPost(zffsbh, zffsmc, zfmoney, pzh, patid, zyh, zh, zhxz, zhbh); 
        }
    }

    //预交金充值
    function PayDepositPost(zffsbh, zffsmc, zfmoney, pzh, patid, zyh, zh, zhxz, zhbh)
    {
        $.loading(true, "正在请求数据...");
        $.najax({
            type: "POST",
            url: "/PatientManage/AccountManage/PayDepositPost",
            data: { zffsbh: zffsbh, zffsmc: zffsmc, zfje: zfmoney, pzh: pzh, zhbh: zhbh, zh: zh,zhxz:zhxz, patid: patid, zyh: zyh },
            dataType: "json",
            cache: false,
            success: function (resdata) {
                if (resdata!=null && resdata.data)
                {
                    $.loading(false);
                    $.modalAlert("充值成功", 'success'); 
                    ClearMoney(); //清空
                    getZhInfo(zh); //账户收支记录
                }
            },
            errorCallback: function (err) { 
                //$.modalAlert("充值失败", 'error');
                $.loading(false);
            },
            complete: function () {
                $.loading(false);
            }
        }); 
    }
 

    //清空
    function ClearMoney() {
        $('#gridList').jqGrid("clearGridData");
        //清空金额输入框
        $("#txtmoney").val("");
        //凭证日期
        var d = new Date();
        var date = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
        $("#txtpzrq").val(date);
        //获取凭证号
        GetSJPZH();
    }

    //获取凭证号
    function GetSJPZH() {
        $.ajax({
            type: "POST",
            url: "/PatientManage/AccountManage/GetReceipt",
            data: { },
            dataType: "json",
            cache: false,
            success: function (resdata) {
                if (resdata != null && resdata.data != "") {
                    $("#txtlpzh").val(resdata.data);
                    return resdata.data;
                } else {
                    $("#txtlpzh").val("");
                    return "";
                }
            }
        });
    }

    //人员查询
    function GetPatSerarchView(zyh, xm, infoList) {
        var encodexm = encodeURI(encodeURI(xm));
        if (infoList) {
            $.modalOpen({
                id: "patSearch",
                title: "患者查询",
                url: "/PatientManage/AccountManage/PatSearchView?t=" + Math.random() + "&zyh=" + zyh + "&xm=" + encodexm,
                width: "700px",
                height: "600px",
                callBack: function (iframeId) { 
                    top.frames[iframeId].PatDbGrid(); //在弹出窗口事件
                }//窗口点确定的回调函数
            });
        } else {
            $.modalAlert("请输入住院号或姓名", 'warning');
            $("#txt_keyword").focus();
        }
    }

    var gridIMoney = "";//当前选中的收支金额
    var gridIpzh = ""; //当前选中的凭证号
    //点击双击事件
    function btn_edit() {
        var szje = $("#gridList").jqGridRowValue().szje;
        var pzh = $("#gridList").jqGridRowValue().pzh;
        gridIMoney = szje;
        gridIpzh = pzh;
        if (parseFloat(gridIMoney)>0)
        {
            $("#txtmoney").val(-szje);
        } else
        {
            gridIMoney = -szje;
            $("#txtmoney").val(szje);
        }
        //$("#lblpzh").text(pzh);
        $("#txtlpzh").val(pzh);
    }

    //退款
    function Refund()
    { 
        var money = $("#txtmoney").val();
        //var pzh = $("#lblpzh").text();
        var pzh = $("#txtlpzh").val();
        var zhye = $("#zhye").val();
        if(parseFloat(money)+parseFloat(gridIMoney)<0){ 
            $.modalAlert("退款金额不能大于当前充值金额", 'warning');
            return false;
        }
        if (parseFloat(money) + parseFloat(zhye) < 0) {
            $.modalAlert("当前账户余额不足", 'warning'); 
            return false;
        }
        if (parseFloat(money) > 0)
        {
            $.modalAlert("如退款请选择一条收支明细", 'warning'); 
            return false;
        } 
        if (gridIpzh == "" || pzh != gridIpzh)
        {
            $.modalAlert("如退款请选择一条收支明细", 'warning'); 
            return false; 
        }
        if (CheckDeposit())
        { 
            ExitDepositPost(zffsbh, zffsmc, zfmoney, pzh, patid, zyh, zh, zhxz, zhbh);
        }
    }


    //退款
    function ExitDepositPost(zffsbh, zffsmc, zfmoney, pzh, patid, zyh, zh, zhxz, zhbh) {
        $.loading(true, "正在请求数据...");
        $.najax({
            type: "POST",
            url: "/PatientManage/AccountManage/ExitDepositPost",
            data: { zffsbh: zffsbh, zffsmc: zffsmc, zfje: zfmoney, pzh: pzh, zhbh: zhbh, zh: zh, zhxz: zhxz, patid: patid, zyh: zyh },
            dataType: "json",
            cache: false,
            success: function (resdata) {
                if (resdata != null && resdata.data) {
                    $.loading(false); 
                    $.modalAlert("退款成功", 'success');
                    ClearMoney();
                    //调用账户收支清单
                    getZhInfo(zh);
                    
                }
            },
            errorCallback: function (err) { 
                $.loading(false);
            },
            complete: function () {
                $.loading(false);
            }
        });
    }

    //查询账户
    function getZhInfo() {
        //GetBasicZhInfoJson
        $.loading(true, "正在请求数据...");
        $.najax({
            type: "POST",
            url: "/PatientManage/AccountManage/GetBasicZhInfoJson",
            data: {zh: zh},
            dataType: "json",
            cache: false,
            success: function (resdata) { 
                if (resdata != null && resdata.data) {
                    $.loading(false);
                    gridListData(resdata.data);
                }
            },
            errorCallback: function (err) {
                $.loading(false);
            },
            complete: function () {
                $.loading(false);
            }
        });
    }



    //重新查询
    function searchPatInfoAgin() {
        //调用账户收支清单
        var obj = new Object();
        obj.zyh = $("#zyh").val();
        obj.kh = $("#kh").val();
        obj.xm = $("#xm").val();
        getPatInfoAjax(obj); //重新调用查询
    }
</script>
